package com.shigen.demo.base;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * stream操作demo
 *
 * @author shigenfu
 * @date 2022/12/2 19:28
 */
public class CollectionDemo {

  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(-88, -451, -958, -966, 32, 887, 335, 207, -479, 113);
    // Collections.sort(list);
    // Collections.shuffle(list);
    //
    // System.out.println(Collections.frequency(list, 207));
    // System.out.println(Collections.max(list));
    //
    // Collections.fill(list, 8);
    // System.out.println(list);
    //
    // System.out.println(Collections.singletonList(0));
    //
    // System.out.println(String.join(",", list.stream().map(Object::toString).collect(Collectors.toList())));

    // =========
    Optional<Integer> first = list.stream().findFirst();
    Integer sum = list.stream().filter(i -> i < 0).distinct().reduce(0, (a, b) -> a + b);
    System.out.println("sum= " + sum);
    Integer multiple = list.stream().filter(i -> i < 0).distinct().reduce(1, (a, b) -> a * b);
    System.out.println("multiple= " + multiple);

    // 并行的线程池处理
    // key:a value: a+1 当冲突的时候，选择a作为key
    LinkedHashMap<Integer, Integer> collect = list.stream().parallel().collect(Collectors.toMap(a -> a, a -> (a + 1), (a, b) -> a, LinkedHashMap::new));
    collect.forEach((k, v) -> System.out.println(k + " ------ " + v));

  }


}
